package org.eae.eldf.framework.redis;

import java.io.Serializable;
import java.util.Collection;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

public class RedisUtils {
	
	
	private ObjectRedisTemplate objectRedisTemplate;
	
	
	private StringRedisTemplate stringRedisTemplate;
	
	/**
	 * 保存字符串到redis
	 * @param key
	 * @param value
	 */
	public void set(String key,String value){
		stringRedisTemplate.opsForValue().set(key, value);
	}
	
	/**
	 * 保存字符串到redis
	 * @param key
	 * @param value
	 * @param minutes 超时分钟
	 */
	public void set(String key,String value,int minutes){
		stringRedisTemplate.opsForValue().set(key, value, minutes, TimeUnit.MINUTES);
	}
	
	/**
	 * 保存序列化对象到redis
	 * @param key
	 * @param value
	 */
	public void set(String key,Serializable obj){
		objectRedisTemplate.opsForValue().set(key, obj);
	}
	
	/**
	 * 保存序列化对象到redis
	 * @param key
	 * @param value
	 * @param minutes 超时分钟
	 */
	public void set(String key,Serializable obj,int minutes){
		objectRedisTemplate.opsForValue().set(key, obj,minutes,TimeUnit.MINUTES);
	}
	
	/**
	 * 获取缓存字符串
	 * @param key
	 * @return
	 */
	public String getString(String key){
		return stringRedisTemplate.opsForValue().get(key);
	}

	/**
	 * 获取缓存对象
	 * @param key
	 * @param clazz
	 * @return
	 */
	public  <T> T getSerialObject(String key){
		return (T)objectRedisTemplate.opsForValue().get(key);
	}
	
	/**
	 * 获取缓存对象
	 * @param key
	 * @return
	 */
	public  Object getObject(String key){
		return objectRedisTemplate.opsForValue().get(key);
	}
	
	/**
	 * 删除缓存
	 * @param key
	 */
	public void delString(String key){
		stringRedisTemplate.delete(key);
	}
	
	/**
	 * 批量删除缓存
	 * @param keys
	 */
	public void delString(Collection<String> keys){
		stringRedisTemplate.delete(keys);
	}
	/**
	 * 删除缓存
	 * @param key
	 */
	public void delObject(String key){
		objectRedisTemplate.delete(key);
	}
	/**
	 * 批量删除缓存
	 * @param keys
	 */
	public void delObject(Collection<String> keys){
		objectRedisTemplate.delete(keys);
	}

	public void setObjectRedisTemplate(ObjectRedisTemplate objectRedisTemplate) {
		this.objectRedisTemplate = objectRedisTemplate;
	}

	public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
		this.stringRedisTemplate = stringRedisTemplate;
	}
	
	
	
}
